The Absolute Beginners Guide To Amos ------------------------------------- Chapter seventeen ------------------ The Answers to The Self Testing Quiz part 4: 1. B 6. A 2. A 7. B 3. A 8. C 4. C 9. B 5. C 10. C =========================================================================== Now I would like to cover DIMensioned Arrays. I know it sounds frightening at first. I can remember trying to get to grips with it myself, but don`t be misled by the name of this command as it`s not as bad as it sounds. It`s the concept that some people can find hard to grasp rather than the usage. THE CONCEPT OF ARRAYS: ---------------------- Arrays are for manipulating large amounts of data or variables. For example a league of 22 football teams could be grouped into an array called team$ we would access a team like this: TEAM$(0) Where if the table was sorted the first team might be Arsenal or Aston Villa for example. Similarly this can also apply to numerical variables we could group together all of the money we spent in the last year on beer in an array called BEER we could then: PRINT BEER (5) This would produce the figure we entered for day five. Or team 5 from the TEAM$ array like this: PRINT TEAM$ (5) Notice the only difference (so far) with a string array and a numerical array is the $ sign. SETTING UP AN ARRAY ------------------- OK so we have 22 teams from the English Premier League and we want to group these into an array called team$. First of all we must reserve some variable space for our array using the DIM command. DIM TEAM$ (21) DIM tells Amos we want to reserve space for an array called team$ which will contain 22 items. Arrays always start at zero, so for 22 teams we would count from 0 to 21. Amos knows it`s a string array as we used a $ after the variable team. If we had put DIM team (21) Amos would presume we wanted a numerical array. Now then, how do we input the data? This is how: DATA "SPURS","ASTON VILLA","CHELSEA","NORWICH" DATA "NEWCASTLE","SWINDON","SHEFF WED","Q.P.R" etc. until we have typed our 22 teams in. Notice the exact format, DATA String in quotes, comma, String in quotes, comma etc. If we were handling our beer numerical array we would enter the numbers like this, DATA 12,17,13,11,12 DATA 22,54,11,0,9,7 etc. Almost the same except no quotes. We have DIMensioned a 22 slot array for our team names and typed the names into DATA statements, but Amos still does not know what to do with all this stuff. At the moment Amos will DIMension your array and ignore the DATA. We have to tell Amos to READ the DATA into the ARRAY. FOR A=0 to 21 READ TEAM$(A) NEXT A We must set up a FOR NEXT loop to read each team from the DATA list into the array. Because we used TEAM$ in the READ line Amos knows we wanted to READ the DATA into our DIMmed array called TEAM$. The beer numerical array would be almost identical. FOR A=0 to 364 READ beer(A) NEXT A Note: Often it is a good idea to put a RESTORE command before you READ the DATA in. This sets the internal DATA pointer to the beginning of the list of DATA. If you get an OUT OF DATA error then RESTORE could solve problem. An example would be: RESTORE league league: DATA "SPURS","CHELSEA" etc. The league: bit is just a label so we can tell Amos what DATA we want to RESTORE because some programs will contain more than one set of DATA statements. So that is the painful part over the rest is plain sailing. The DATA is now safely in the array and you could access it like this: PRINT TEAM$(1) SPURS <<< (This is what would be PRINTed on screen) PRINT TEAM$(2) ASTON VILLA PRINT TEAM$ (3) CHELSEA And the beer array, PRINT beer(1) 12 PRINT beer(2) 17 PRINT beer (3) 13 But if we wanted to print all 22 of our teams on the screen, a neater and more practical way would be to use a FOR NEXT loop similar to the one we used to READ the DATA in. FOR A=0 TO 21 PRINT TEAM$(A) NEXT A This would print all 22 team names on the screen in a whisker. To do this to the beer array we could just substitute the 21 for 364 and TEAM$ for beer. One of the many bonuses of using of arrays is that we can use the Amos SORT command, which is fast and saves a lot of coding. If we want to SORT our array all we do is this: SORT TEAM$ (0) The zero is the start of our array. For the beer array it`s, SORT beer (0) And the teams will all be sorted in alphabetical order and the beer numbers will be sorted in ascending order in an instant and all in one command. There is a lot more to do with arrays than this little tutorial could possibly cover and I don`t want to go any further at this stage but you now have enough knowledge to use simple arrays in your programs. Before we continue on to a new subject you may want to take a look at EXAMPLE17.Amos which uses our team$ idea. The example program also covers the next part of this chapter which is all to do with PROCEDURES. While DIM, DATA and such like are still fresh in your mind take a browse at it. PROCEDURES ---------- Think of procedures as self contained programs a bit like subroutines. You can write a small part of your program and fold it out of sight into one line making your listings easier to read and easier to debug and program. Procedures are well worth taking the time to learn how to use and once you get in the habit of using procedures you will always use them. Here is a part of a program that we would probably want to use many times during the course of the program: WHILE MOUSE KEY=0: WEND Let`s presume there are five places in the program we need to use a mouse wait (which is what this line does, remember?) We could have five separate lines in the program we could use GOSUB or better we could put it in a PROCEDURE, this is how it`s done. PROCEDURE _mousewait WHILE MOUSEKEY=0: WEND END PROC And that`s it, obviously the bigger the program we put in the procedure the more effective it becomes. We can now call our procedure with: Proc _mousewait Or just: _mousewait And the procedure will be executed and return to the next command after the line we originally called the procedure from. I could of called the procedure anything I wanted but being descriptive helps later when your programs grow in size. There are quite a few related commands to procedures but as usual I will just cover the ones we really need to know about for now, so here goes. POP PROC --------- If you need to return from a procedure early for some reason this is your man, an example could be: IF A>5 then POP PROC In other words if the A was more than five then end the procedure. Don`t forget though that you still have to put END PROC at the END of your PROCedure even if you do use POP PROC The only other procedure related command we need to know about at the moment is: GLOBAL ------ When you put a program inside a procedure the procedure itself is a self contained module and any variables used inside that procedure will not effect any variables outside it even if they have the same name, unless that is, we assign them to be GLOBAL variables which means Amos will treat the following variables the same inside and out of procedures, an example. GLOBAL a$,team$(),beer(),f This line makes a string called A$ an array called team$ an array called beer and a lone variable called f all GLOBAL. Notice that to make an array GLOBAL you must put () after it or GLOBAL will treat it as a normal variable. OK that covers it on a need to know basis. Now reload EXAMPLE17.Amos and you should understand the whole program a lot better. End of chapter seventeen ^^^^^^^^^^^^^^^^^^^^^^^^